package day01;

import org.activiti.engine.*;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Comment;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.List;

public class ActivitiDemo {

    //部署方法  --->  流程定义
    @Test
    public void testDeploy(){
        //获取引擎流对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deploy = repositoryService.createDeployment()         //创建部署对象
                .addClasspathResource("BPMN/leaveProcess.bpmn20.xml")    //部署什么资源
                .deploy();                                               //进行部署
        System.out.println("部署ID : "+deploy.getId());
        System.out.println("部署时间 : "+deploy.getDeploymentTime());
        System.out.println("部署时间 : "+deploy.getKey());

    }
    @Test
    public void testStart(){
        //获取引擎流对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = processEngine.getRuntimeService();
        //启动流程
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("leaveProcess");
        System.out.println("流程实例ID : " + processInstance.getId());
        System.out.println("流程实例ID : " + processInstance.getProcessInstanceId());
        System.out.println("流程定义ID : " + processInstance.getProcessDefinitionId());
        System.out.println("流程定义Key : " + processInstance.getProcessDefinitionKey());


    }
    //查询待办任务
    @Test
    public  void testQueueuery(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        List<Task> taskList = taskService.createTaskQuery()
                .taskAssignee("张三")
                .processDefinitionKey("leaveProcess")
                .list();
        for (Task task : taskList) {
            System.out.println("当前任务ID : " + task.getId());
            System.out.println("当前任务名称 : " + task.getName());
            System.out.println("当前任务负责人 : " + task.getAssignee() );
            System.out.println("当前任务ID创建时间 : " + task.getCreateTime().toLocaleString() );
            System.out.println("当前任务ID对应的流程实例ID : " + task.getProcessDefinitionId());
            System.out.println("当前任务ID对应的流程定义ID : " + task.getProcessInstanceId());
            System.out.println("❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤");
        }

    }

    //任务审批
    @Test
    public  void testCompleteTask(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        taskService.complete("2505");
    }
    // 完成任务同时添加批注信息
    @Test
    public void testCompleteTaskAndComment() {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();

        taskService.addComment("5005", "5001", "哦同意通过");
        taskService.complete("5005");
    }
    // 查询历史任务及任务审批
    @Test
    public void testQueryHistoryTaskAndComment(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        HistoryService historyService = processEngine.getHistoryService();
        TaskService taskService = processEngine.getTaskService();
        List<HistoricTaskInstance> historicTaskInstanceList = historyService.createHistoricTaskInstanceQuery()
                .processDefinitionKey("leaveProcess")
                .processInstanceId("5001")
                .finished().list();
        for (HistoricTaskInstance list : historicTaskInstanceList) {
            System.out.println("历史任务ID：" + list.getId());
            System.out.println("历史任务名称：" + list.getName());
            System.out.println("历史任务负责人：" + list.getAssignee());
            System.out.println("历史任务创建时间：" + list.getCreateTime().toLocaleString());
            System.out.println("历史任务开始时间：" +list.getStartTime());
            System.out.println("历史任务结束时间：" + list.getEndTime());
            System.out.println("历史任务总耗时：" + list.getDurationInMillis()/1000/60 + "分钟");
            System.out.println("历史任务对应流程实例ID：" +list.getProcessInstanceId());
            System.out.println("历史任务对应流程定义ID：" +list.getProcessDefinitionId());
            List<Comment> taskComments = taskService.getTaskComments(list.getId());
            if(taskComments != null && taskComments.size() > 0){
                for (Comment comment : taskComments) {
                    System.out.println("当前任务批注信息  :  " + comment.getFullMessage() );
                }
            }else {
                System.out.println("没有批注信息");
            }
            System.out.println("❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤");
        }
    }
    //上传流程图片
    @Test
    public void testDeployAndPng(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("BPMN/leaveProcess.bpmn20.xml")
                .addClasspathResource("BPMN/diagram.png")
                .deploy();
        System.out.println("部署ID : " + deploy.getId());
        System.out.println("部署时间 : " + deploy.getDeploymentTime());

    }
    //删除流程定义
    @Test
    public void deleteProcessDefinition(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();
        // 删除指定流程定义  一次部署 -->  一个流程定义   删除时需要部署ID
        // 部署ID
        String deploymenetId = "1";
        // 若该流程定义下面没有正在执行的流程实例，则可以直接删除。
        // repositoryService.deleteDeployment(deploymenetId);
        // 若该流程定义下面又正在执行的流程实例，则直接删除抛异常。默认值 false 。我们需要将 cascade 设置为 true
        repositoryService.deleteDeployment(deploymenetId,true);

    }
}
